package com.haruhinav.service.impl;

import com.haruhinav.entity.Message;
import com.haruhinav.entity.vo.ReplyMsgVO;
import com.haruhinav.entity.extend.MessageEX;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.mapper.MessageMapper;
import com.haruhinav.service.MessageService;
import com.haruhinav.Utils.TimeTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageMapper messageMapper;
    @Autowired
    private TimeTool timeTool;

    @Override
//    msgex:分页的条件 1.当前页码 2.每页大小
    public Map<String,Object> getMessagelimit(MessageEX msgex) {
        Map<String,Object> msgMap = new HashMap<>();
        //(页码 - 1 ) * size
        msgex.setPage((msgex.getPage()-1)*msgex.getPageSize());
        List<Message> msgs = messageMapper.getMessagelimit(msgex);//留言 (每次查5条)
        //SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        //String transformDate=simpleDateFormat.format();
        //yyyy-MM-dd hh:mm:ss -> yyyy年MM月dd日 hh:mm
        msgs.forEach(msg->msg.setCreateTime(timeTool.SubTime(msg.getCreateTime())));
        msgMap.put("msgList",msgs);
        msgMap.put("msgCount",messageMapper.countAllMessage());//留言总数
        return msgMap;
    }

    @Override
    public boolean insertMsg(Message message) {

        //Random random = new Random();
        int x =1+(int)(Math.random()*50);
        String f_url = "http://www.cilicili.cc/caozha-comment/assets/userpic/"+ x +".gif";
        message.setUid(message.getUid()==null ? 0:message.getUid());
        message.setMsgNickName(message.getMsgNickName()=="" ? "匿名游客":message.getMsgNickName());
        message.setMsgEmail(message.getMsgEmail()=="" ? null : message.getMsgEmail());
        message.setContext(message.getContext()==""? null:message.getContext());
        message.setIp(message.getIp()=="" ? null:message.getIp());
        //如果没有填写头像url,则使用默认头像
        message.setMsgFace(message.getMsgFace()==null  || message.getMsgFace().trim()==""? f_url:message.getMsgFace());
        //执行插入数据（这里是返回数据库受影响行数）
        messageMapper.insertMsg(message);
        return message.getId() != 0;//插入数据的id映射进原数据
    }

//    回复
    @Override
    public boolean replyMsg(ReplyMsgVO replyMsg) {
        int x =1+(int)(Math.random()*50);
        String f_url = "http://www.cilicili.cc/caozha-comment/assets/userpic/"+ x +".gif";
        replyMsg.setMsgNickName(replyMsg.getMsgNickName()=="" ? "匿名游客":replyMsg.getMsgNickName());
        replyMsg.setContext(replyMsg.getContext()==""? null:replyMsg.getContext());
        //如果没有填写头像url,则使用默认头像
        replyMsg.setMsgFace(replyMsg.getMsgFace()==null  || replyMsg.getMsgFace().trim()==""? f_url:replyMsg.getMsgFace());
        messageMapper.replyMap(replyMsg);

        return replyMsg.getId()!=0;
    }

    @Override
    public Map<String,Object> getAllMsg(MessageEX messageEX) {

        messageEX.setPage((messageEX.getPage()-1)*messageEX.getPageSize());

        Map<String,Object> msgMap = new HashMap<>();
        List<Message> result = new ArrayList<>();
        //所有第一级评论 查询ReplyId为0的数据
        List<Message> fmsgs = messageMapper.getAllTopOneMsg(messageEX);
        for(Message msg:fmsgs){

            //查询第一级评论下的所有回复 msg.getId()是源（第一级）评论id
            List<ReplyMsgVO> replyMsgs = messageMapper.findMsgBySourceMid(msg.getId());
            List<ReplyMsgVO> reply = new ArrayList<>();
            //添加被回复人的name
            for(ReplyMsgVO rm:replyMsgs){
                //根据被回复id获取msg对象
                Message msgobj = messageMapper.findMessageById(rm.getReplyId());
                rm.setReplyName(msgobj.getMsgNickName());
                rm.setCreateTime(timeTool.SubTime(rm.getCreateTime()));
                //将回复对象放进回复集合
                reply.add(rm);
            }
            msg.setCreateTime(timeTool.SubTime(msg.getCreateTime()));
            //将回复集合放进评论对象(该对象为第一级评论)
            msg.setReplyMsgs(reply);
            //将第一级评论对象放进评论集合
            result.add(msg);
        }
        msgMap.put("msgList",result);
        msgMap.put("msgCount",messageMapper.countMessage());//留言总数 加上回复的
        msgMap.put("pageCount",messageMapper.countTopOneMsg());//第一级留言（评论）总数
        return msgMap;
    }

    @Override
    public ResultVO delMsgById(Message msg) {
        ResultVO result = new ResultVO();
        if(msg.getReplyId()==msg.getSourceMsgId()){
            messageMapper.delMsgBySourceId(msg.getId());
        }else{
            messageMapper.delMsgByReplyId(msg.getId());
        }

        if( messageMapper.delMsgById(msg.getId())>0){

            result.setCode(0);
            result.setData(msg);
            result.setMsg("删除成功");
            return result;
        }
        result.setCode(-1);
        result.setData(msg);
        result.setMsg("删除失败");
        result.setType("error");
        return result;
    }
}
